package net.easyjava.framework;

import net.easyjava.framework.web.security.UserSecurityAdminOnCookie;
import net.easyjava.framework.web.security.UserSecurityBean;
import net.easyjava.framework.web.security.UserSecurityOnCookie;
import net.easyjava.framework.web.security.UserSecurityOnHead;
import net.easyjava.tools.Base;
import net.easyjava.tools.http.request.RequestTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by wxnacy on 15/12/22.
 */
public class BaseManager extends Base {
    private static final long serialVersionUID = -2395234890830211321L;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private UserSecurityOnHead userSecurityOnHead;

    @Resource
    private UserSecurityOnCookie userSecurityOnCookie;
    @Resource
    private UserSecurityAdminOnCookie userSecurityAdminOnCookie;

    /**
     * 获取版本号
     * @return
     */
    protected String getVersion(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        return RequestTools.getHeader(request,"version");
    }

    /**
     * 获取请求来源
     * @return
     */
    protected String getSource(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        return RequestTools.getHeader(request,"source");
    }


    /**
     * 获取登陆用户id
     * @return
     */
    @Deprecated
    protected UserSecurityBean getUserSecurityBean(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        UserSecurityBean userSecurityBean  = this.userSecurityOnHead.getUserSecurityBean(request);
        if(userSecurityBean == null){
            return null;
        }
        return userSecurityBean;
    }

    /**
     * 将用户数据放入网络环境中 不过期
     * @param userId
     * @param status
     * @param request
     * @param response
     */
    protected void setLoginUserSecurity(String userId,int status,HttpServletRequest request,HttpServletResponse response){
        this.userSecurityOnHead.setLongUserSecurityBean(userId,status,request,response);
        this.userSecurityOnCookie.setLongUserSecurityBean(userId,status,request,response);
    }

    /**
     * 将admin用户数据放入到cookie中  一个月过期
     * @param userId
     * @param status
     * @param request
     * @param response
     */
    protected void setAdminLoginUserSecurityOnCookie(String userId,int status,HttpServletRequest request,HttpServletResponse response){
        this.userSecurityAdminOnCookie.setLongUserSecurityBean(userId,status,request,response);
    }

    /**
     * 页面输出
     * @param message
     * @param response
     */
    public void printWriter(String message,HttpServletResponse response){
        try {
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            //这句话的意思，是告诉servlet用UTF-8转码，而不是用默认的ISO8859
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            out.println(message);	//请不要修改或删除
            out.flush();
            out.close();
        } catch (IOException e) {
            logger.error(e.getMessage(), e.fillInStackTrace());
        }
    }

}
